<?php
class User_Model extends CI_Model {
  function __construct() {
    $this->load->database ();
  }
  
  function list_all() {
//     $this->db->order_by("$sortfield", "$order");
    $query = $this->db->get_where ( 'user', array('delete_status' => 0));
    return $query->result_array ();
  }
  function export() {
      $query = $this->db->order_by("id", "asc")->get_where ( 'user', array('delete_status' => 0));
      return $query->result_array ();
  }
  function count_all() {
    $this->db->where('delete_status',"0");
    $this->db->from('user');
    return $this->db->count_all_results();
  }
  
  function api_login($username, $password) {
    try {
      $this->db->select ( '*' )
        ->from ( 'user' )
        ->where ( 'login_name', $username )
//         ->where ( 'login_password', md5($password ))
        ->where ( 'login_password', $password )
        ->where ( 'delete_status', 0 );
      $get_user = $this->db->get ()->row();
      if (0!=count($get_user)) {
        return $get_user;
      } else {
        return null;
      }
    } catch ( Exception $ex ) {
      return null;
    }
  }
  function get_by_id($user_id) {
    try {
      $this->db->select ( '*' )
        ->from ( 'user' )
        ->where ( 'id', $user_id );
      $query = $this->db->get ();
      return $query->result ();
    } catch ( Exception $ex ) {
      return false;
    }
  }
  function create($data) {
    try {
      $data_insert = array ();
      foreach ( $data as $k => $v ) {
//           if ('login_password' == $k){
//             $data_insert [$k] = md5($v);
//           } else {
            $data_insert [$k] = $v;
//           }
        }
      $data_insert['created_date']= strtotime(date("Y/m/d H:i:s"));
      $data_insert['updated_date']=  strtotime(date("Y/m/d H:i:s"));
      $data_insert['delete_status']=  0;
      $this->db->insert ( 'user', $data_insert );
      return true;
    } catch ( Exception $ex ) {
      return false;
    }
  }
  function update($user_id, $data) {
    try {
      $data_update = array ();
      foreach ( $data as $k => $v ) {
        $data_update [$k] = $v;
      }
      // set login info
      $old_data = $this->get_by_id ( $user_id );
//       $data_update ['login_name'] = $old_data [0]->login_name;
//       $data_update ['login_password'] = $old_data [0]->login_password;
      $data_update ['delete_status'] = $old_data [0]->delete_status;
      $data_update['created_date']=  $old_data [0]->created_date;
      $data_update['updated_date']=  strtotime(date("Y/m/d H:i:s"));
      $this->db->where ( 'id', $user_id );
      $this->db->update ( 'user', $data_update );
      return true;
    } catch ( Exception $ex ) {
      return false;
    }
  }
  function change_login_info($user_id, $data) {
    try {
      $data_update = array ();
      // set pass
      $old_data = $this->get_by_id ( $user_id );
      foreach ( $old_data [0] as $k => $v ) {
        $data_update [$k] = $v;
      }
//       $data_update ['login_password'] = md5($data['login_password']);
      $data_update ['login_name'] = $data['login_name'];
      $data_update ['login_password'] = $data['login_password'];
      $data_update['updated_date']=  strtotime(date("Y/m/d H:i:s"));
      $this->db->where ( 'id', $user_id );
      $this->db->update ( 'user', $data_update );
      return true;
    } catch ( Exception $ex ) {
      return false;
    }
  }
  function delete($user_id){
    try {
      $data_update = array ();
      // set pass
      $old_data = $this->get_by_id ( $user_id );
      foreach ( $old_data[0] as $k => $v ) {
        $data_update [$k] = $v;
      }
      $data_update ['delete_status'] = 1;
      $this->db->where ( 'id', $user_id );
      $this->db->update ( 'user', $data_update );
      return true;
    } catch ( Exception $ex ) {
      return false;
    }
  }
  function import($data) {
      try {
          $data['created_date']=strtotime(date("Y/m/d H:i:s"));
          $data['updated_date']=strtotime(date("Y/m/d H:i:s"));
          $data['delete_status']= 0;
          $this->db->insert ( 'user', $data );
          return true;
      } catch ( Exception $ex ) {
          return false;
      }
  }
}
?>